package com.darren.befriend.utilities;

import java.util.ArrayList;
import java.util.List;

import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class ParseUtility {
	public static final int MALE = 0;
	public static final int FEMALE = 1;

	public static double getDistance(ParseUser currentUser, ParseUser user) {
		if (currentUser != null && user != null) {
			ParseGeoPoint pos = (ParseGeoPoint) user.get("Position");
			ParseGeoPoint pos1 = (ParseGeoPoint) currentUser.get("Position");
			if (pos != null && pos1 != null) {

				return pos1.distanceInMilesTo(pos);
			}
		}

		return -1;
	}

	public static String getFullName(ParseUser user) {
		if (user != null) {

			String fullName = (String) user.get("fullName");
			return fullName;
		}

		return "";
	}

	public static String getNameAndAge(ParseUser user) {
		if (user != null) {
			try {

				String fullName = (String) user.get("fullName");
				ParseObject meInfo = (ParseObject) user.get("MeInfo");
				if (fullName.contains("_local")) {

					return fullName.replace("_local", "") + ","
							+ (Integer) meInfo.get("MyAge");
				}

				return fullName + "," + (Integer) meInfo.get("MyAge");
			} catch (Exception ex) {

				return "";
			}
		}

		return "";
	}

	public static boolean getStatus(ParseUser user) {
		if (user != null) {

			return (Boolean) user.get("Status");
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	public static List<Boolean> getHobbies(ParseUser user) {
		List<Boolean> hobbies = new ArrayList<Boolean>();
		if (user != null) {

			try {
				ParseObject myMeInfo = (ParseObject) user.get("MeInfo");
				if (myMeInfo != null) {

					hobbies = (List<Boolean>) myMeInfo.get("Hobby");
				}
			} catch (Exception ex) {

			}
		}

		return hobbies;
	}

	public static int getGender(ParseUser user) {
		ParseObject meInfo;

		meInfo = ((ParseObject) user.get("MeInfo"));
		if (meInfo == null) {

			return -1;
		}

		if ((Integer) meInfo.get("Gender") == MALE) {

			return MALE;
		} else {

			return FEMALE;
		}
	}

	public static int getPersonality(ParseUser user) {
		ParseObject meInfo = (ParseObject) user.get("MeInfo");
		if (meInfo == null) {

			return -1;
		}

		return (Integer) meInfo.get("Personality");
	}

	public static ParseObject getMeInfo(ParseUser user) throws ParseException {

		return ((ParseObject) user.get("MeInfo")).fetchIfNeeded();
	}
}
